package cn.csust.net2.manager.server.service.impl;

import java.util.List;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.service.VoteManagerService;
import cn.csust.net2.manager.shared.util.ServiceConstant;

import com.extjs.gxt.ui.client.data.PagingLoadConfig;
@Service(value = ServiceConstant.SERVICE_NAME_VOTEMANAGER)
public class VoteManagerServiceImpl extends VoteCommonServiceImpl implements VoteManagerService{
	@Transactional
	@Override
	public void saveVoteBanjiIDListByPaging(PagingLoadConfig config)throws BeyondAuthorityException {
		
		Long voteID = config.get(VoteManagerService.VOTEID);
		List<Integer>	numberList = config.get(BANJIIDLIST);
		if(voteID!=null){
			String sql = "DELETE FROM `manager`.`banji_vote` WHERE voteID = " + voteID;
			SQLQuery q = getVoteDAO().getCurrentSession().createSQLQuery(sql);
			q.executeUpdate();
			if(numberList!=null){
				for(Integer numberID:numberList){
					String sqll = "INSERT INTO `manager`.`banji_vote`(`banjiID`,`voteID`) VALUES ( "+numberID+","+voteID+");";
					SQLQuery ql = getVoteResultDAO().getCurrentSession().createSQLQuery(sqll);
					ql.executeUpdate();
				}
			}
		}
		
	}

	@Transactional
	@Override
	public List<Integer> findVoteBanjiIDListByPaging(PagingLoadConfig config)throws BeyondAuthorityException {
		Long voteID = config.get(VoteManagerService.VOTEID);
		if(voteID!=null){
			String sql = "SELECT banjiID FROM `manager`.`banji_vote` WHERE voteID = " + voteID;
			List<Integer> list = getVoteDAO().findListBySQL(sql);			
			return list;
		}
	
		 
		return null;
	}
	
}
